package com.zyms;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import com.zyms.factory.SMSTemplateCodeFactoty;
import com.zyms.module.sms.SMSBase;
import com.zyms.module.sms.SMSResult;
import com.zyms.myRunTime.MyRunTimeExcption;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by Baggio on 2017-11-21.
 */
public class SMSTools {
    private static final String url = "http://gw.api.taobao.com/router/rest";

    private static final String appkey = "23628936";
    private static final String secret = "dc0a371a6b2f5d87a14974708a5d9299";
    private static final String SMS_TYPE ="normal";
    private static final String SMS_FREE_SIGN_NAME ="星艺网";

    /**
     * 短信发送
     * @param smsBean
     * @return
     * @throws ApiException
     */
    public static SMSResult sendSMS(SMSBase smsBean) throws ApiException {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setSmsType(SMS_TYPE);
        req.setSmsFreeSignName(SMS_FREE_SIGN_NAME);
        System.out.println(smsBean.toJSON());
        req.setSmsParamString(smsBean.toJSON());
        String phone=smsBean.getPhone();

        if(StringUtils.isBlank(phone)){
            throw new MyRunTimeExcption("电话号码不能为空！");
        }
        req.setRecNum(phone);
        String templateCode= SMSTemplateCodeFactoty.getTemplateCode(smsBean.getTemplateCode());
        if(StringUtils.isBlank(templateCode)){
            throw new MyRunTimeExcption("短信模板号不能为空！");
        }
        req.setSmsTemplateCode(templateCode);
        AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
        SMSResult smsResult=new SMSResult();
        smsResult.setErrorCode(rsp.getErrorCode());
        smsResult.setErrorMSG(rsp.getSubMsg());
        return smsResult;
    }

}
